package com.example.food.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.food.activity.OrderActivity;
import com.example.food.activity.ProductsettlementActivity;
import com.example.food.bean.AlacarteBean;
import com.example.food.bean.OrderListDetailsBean;
import com.example.home.R;

import java.util.List;

import static com.example.home.tool.StaticActi.ip;

public class FoodProductsAdapter extends RecyclerView.Adapter<FoodProductsAdapter.Myhot> {

    private Context context;
    private List<AlacarteBean.DataBean> list;
    private List<OrderListDetailsBean.DataBean.OrderInfoBean.OrderItemListBean>  listDTOS;

    public FoodProductsAdapter(Context context, List<OrderListDetailsBean.DataBean.OrderInfoBean.OrderItemListBean>  listDTOS,String TYPE) {
        this.context = context;
        this.listDTOS = listDTOS;
    }

    @NonNull
    @Override
    public FoodProductsAdapter.Myhot onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(R.layout.item_products,null);
        return new FoodProductsAdapter.Myhot(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull FoodProductsAdapter.Myhot holder, int position) {
        if(context instanceof  ProductsettlementActivity){
            if(list.get(position).getCounter()!=0){
                Glide.with(context).load(ip+list.get(position).getImgUrl()).into(holder.imgurl);
                holder.cainame.setText(list.get(position).getName());
                holder.num.setText(String.valueOf(list.get(position).getCounter()));
                Double money  = list.get(position).getCounter()*list.get(position).getPrice();
                holder.price.setText(String.valueOf(money));
            }
        }else if(context instanceof OrderActivity){
            Glide.with(context).load(ip+listDTOS.get(position).getProductImage()).into(holder.imgurl);
            holder.cainame.setText(listDTOS.get(position).getProductName());
            holder.num.setText(String.valueOf("x"+listDTOS.get(position).getQuantity()));
            Double money  = listDTOS.get(position).getQuantity()*listDTOS.get(position).getProductPrice();
            holder.price.setText(String.valueOf(money));
        }

    }

    @Override
    public int getItemCount() {
        if(context instanceof ProductsettlementActivity){
            return list==null?0:list.size();
        }else {
            return listDTOS==null?0:listDTOS.size();
        }

    }

    class Myhot extends RecyclerView.ViewHolder{

        private final ImageView imgurl;
        private final TextView cainame;
        private final TextView num;
        private final TextView price;

        public Myhot(@NonNull View itemView) {
            super(itemView);
            imgurl = itemView.findViewById(R.id.imgurl);
            cainame = itemView.findViewById(R.id.cainame);
            num = itemView.findViewById(R.id.num);
            price = itemView.findViewById(R.id.price);
        }
    }
}
